//
//  ECAddCompanyViewController.m
//  ECERP.CRM
//
//  Created by Mac on 2018/10/19.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "ECAddCompanyViewController.h"
#import "ECAddCompanyTableViewCell.h"
#import "ECCompanyModel.h"
#import "LZCacheTool.h"
#define KCompanyInfoCacheName @"companyIpInfoCacheName"  //缓存cache名字

@interface ECAddCompanyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIButton *addBtn;

@end

@implementation ECAddCompanyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"添加公司";
    
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(0, 0, 14, 14);
    [btn setImage:[UIImage imageNamed:@"return_icon"] forState:UIControlStateNormal];
    [btn addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.addBtn];
    
}

-(void)yycacheTest
{
    YYCache *cache = [YYCache cacheWithName:@"mydb"];
    NSArray *array=@[@"11",@"12",@"13",@"14",@"15"];
    
    for (int i=0; i<array.count; i++) {
        ECCompanyModel *model=[[ECCompanyModel alloc]init];
        model.companyName=[array objectAtIndex:i];
    }
    
    //存储
    [cache setObject:array forKey:@"user" withBlock:^{
        NSLog(@"array缓存完成....");
    }];
    
    
    // 读取
    [cache objectForKey:@"user" withBlock:^(NSString * _Nonnull key, id<NSCoding>  _Nonnull object) {
        NSLog(@"%@", object);
    }];
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ECAddCompanyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    switch (indexPath.row) {
        case 0:
            cell.nameLab.text=@"公司名称";
            cell.textF.placeholder=@"请输入公司名称";
            break;
            
        case 1:
            cell.nameLab.text=@"域名";
            cell.textF.placeholder=@"请输入域名";
            break;
        case 2:
            cell.nameLab.text=@"地址";
            cell.textF.placeholder=@"请输入地址";
            break;
        case 3:
            cell.nameLab.text=@"联系方式";
            cell.textF.placeholder=@"请输入联系方式";
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, ECSCREEN_WIDTH, 49*4) style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ECAddCompanyTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.rowHeight=48;
    }
    return _tableView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    [self yycacheTest];

}


- (UIButton *)addBtn
{
    if (!_addBtn) {
        _addBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame=CGRectMake(12, _tableView.bottom+40, ECSCREEN_WIDTH-24, 49);
        [_addBtn setTitle:@"提交" forState:UIControlStateNormal];
        _addBtn.titleLabel.font=ECFONT(16);
        _addBtn.backgroundColor=ECCOLOR_NAVIBACK;
        _addBtn.layer.cornerRadius=12.0;
        [_addBtn addTarget:self action:@selector(commitClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(void)commitClickAction
{
    if ([self getCellTextFieldText:0].length==0) {
         [MBProgressHUD showText:@"请输入公司名称" toView:self.view];
        return;
    }
    
    if ([self getCellTextFieldText:1].length==0) {
        [MBProgressHUD showText:@"请输入域名" toView:self.view];
        return;
    }
    if ([self getCellTextFieldText:2].length==0) {
        [MBProgressHUD showText:@"请输入地址" toView:self.view];
        return;
    }
    if ([self getCellTextFieldText:3].length==0) {
        [MBProgressHUD showText:@"请输入联系方式" toView:self.view];
        return;
    }
    
    NSDictionary *dict=@{@"companyName":[self getCellTextFieldText:0],
                         @"companyIP":[self getCellTextFieldText:1],
                         @"companyAddress":[self getCellTextFieldText:2],
                         @"companyPhone":[self getCellTextFieldText:3]
                         };
    ECCompanyModel *model=[[ECCompanyModel alloc]initWithDictionary:dict];
    
    
    NSMutableArray *tempArray=[NSMutableArray arrayWithObject:model];
    
    //缓存
    [LZCacheTool cacheForData:tempArray fileName:KCompanyInfoCacheName];
    [MBProgressHUD showSuccess:@"提交成功"];
    
    //发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"KCacheCompanySuccess" object:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSString *)getCellTextFieldText:(NSInteger )row
{
    ECAddCompanyTableViewCell *cell=[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
    return cell.textF.text;
}

@end
